package demo.openvc.com.openvcdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.widget.TextView;


import java.util.Date;
import java.util.concurrent.TimeUnit;

import demo.openvc.com.utils.DateUtils;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by linaidao on 2018/12/25.
 */

public class SplashAct extends Activity {

    private TextView tvTime;
    private TextView tvDate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.act_splash);

        this.tvDate = (TextView) findViewById(R.id.tvDate);
        this.tvTime = (TextView) findViewById(R.id.tvTime);
        countDown();
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(3000);

                Intent m = new Intent(SplashAct.this, MainActivity.class);
                startActivity(m);

                if (disposable != null)
                    disposable.dispose();

                finish();
            }
        }).start();

    }

    private Disposable disposable;

    /**
     * 倒计时
     */
    private void countDown() {

        Observable.interval(0, 1, TimeUnit.SECONDS, AndroidSchedulers.mainThread()).map(new Function<Long, String>() {
            @Override
            public String apply(Long aLong) throws Exception {
                return "计时";
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(String value) {

                        tvTime.setText(DateUtils.formatDateTime(new Date(), DateUtils.DF_HH_MM_SS));
                        tvDate.setText(DateUtils.formatDateTime(new Date(), DateUtils.DF_xYYYYx_MMx_DDx)
                                + "  " + DateUtils.getWeek());

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
